﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;
using ThreeByte.Media;

namespace ThreeByte.ActiveDeck.AddIn.Content.Controls
{
    public class DesignerResizeThumb : Thumb
    {
        public DesignerResizeThumb() {
            DragDelta += new DragDeltaEventHandler(DesignerResizeThumb_DragDelta);
        }

        void DesignerResizeThumb_DragDelta(object sender, DragDeltaEventArgs e) {

            DesignerControl designerItem = this.DataContext as DesignerControl;

            if(designerItem != null) {
                double deltaVertical, deltaHorizontal;

                switch(VerticalAlignment) {
                    case VerticalAlignment.Bottom:
                        deltaVertical = Math.Min(-e.VerticalChange, designerItem.CurrentPixelSpace.H);
                        //designerItem.CurrentPixelSpace.H -= (int)deltaVertical;
                        designerItem.CurrentPixelSpace.H = (int)Math.Round(PixelSpace.ClampHeight(designerItem.CurrentPixelSpace, Bounds, designerItem.CurrentPixelSpace.H - deltaVertical));
                        break;
                    case VerticalAlignment.Top:
                        deltaVertical = Math.Min(e.VerticalChange, designerItem.CurrentPixelSpace.H);
                        //designerItem.CurrentPixelSpace.Y += (int)(deltaVertical);
                        //designerItem.CurrentPixelSpace.H -= (int)deltaVertical;
                        designerItem.CurrentPixelSpace.Y = (int)Math.Round(PixelSpace.ClampTop(designerItem.CurrentPixelSpace, Bounds, designerItem.CurrentPixelSpace.Y + deltaVertical));
                        designerItem.CurrentPixelSpace.H = (int)Math.Round(PixelSpace.ClampHeight(designerItem.CurrentPixelSpace, Bounds, designerItem.CurrentPixelSpace.H - deltaVertical));
                        break;
                    default:
                        break;
                }

                switch(HorizontalAlignment) {
                    case HorizontalAlignment.Left:
                        deltaHorizontal = Math.Min(e.HorizontalChange, designerItem.CurrentPixelSpace.W);
                        //designerItem.CurrentPixelSpace.X += (int)(deltaHorizontal);
                        //designerItem.CurrentPixelSpace.W -= (int)deltaHorizontal;
                        designerItem.CurrentPixelSpace.X = (int)Math.Round(PixelSpace.ClampLeft(designerItem.CurrentPixelSpace, Bounds, designerItem.CurrentPixelSpace.X + deltaHorizontal));
                        designerItem.CurrentPixelSpace.W = (int)Math.Round(PixelSpace.ClampWidth(designerItem.CurrentPixelSpace, Bounds, designerItem.CurrentPixelSpace.W - deltaHorizontal));
                        break;
                    case HorizontalAlignment.Right:
                        deltaHorizontal = Math.Min(-e.HorizontalChange, designerItem.CurrentPixelSpace.W);
                        //designerItem.CurrentPixelSpace.W -= (int)deltaHorizontal;
                        designerItem.CurrentPixelSpace.W = (int)Math.Round(PixelSpace.ClampWidth(designerItem.CurrentPixelSpace, Bounds, designerItem.CurrentPixelSpace.W - deltaHorizontal));
                        break;
                    default:
                        break;
                }
            }

            e.Handled = true;
        }

        public static readonly DependencyProperty BoundaryProperty = DependencyProperty.Register("Bounds",
            typeof(Boundary), typeof(DesignerResizeThumb), new FrameworkPropertyMetadata(Boundary.Universe));

        public Boundary Bounds {
            get {
                return (Boundary)GetValue(BoundaryProperty);
            }
            set {
                SetValue(BoundaryProperty, value);
            }
        }
    }
}
